/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * form.java
 *
 * Created on Oct 18, 2011, 10:52:16 PM
 */
package recursionassignment.des.assignment3;

import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import java.util.Calendar;


/**
 *
 * @author CantCatchMe
 */
public class form extends javax.swing.JFrame {
     public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
     String strOutput = ""; //for the last action performed log on screen 
    
    public form() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        logPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        output = new javax.swing.JTextArea();
        buttonPanel = new javax.swing.JPanel();
        exponentialButton = new javax.swing.JButton();
        gcdButton = new javax.swing.JButton();
        factorialButton = new javax.swing.JButton();
        placeButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 51, 51));

        output.setBackground(new java.awt.Color(255, 255, 204));
        output.setColumns(20);
        output.setEditable(false);
        output.setRows(5);
        jScrollPane1.setViewportView(output);

        javax.swing.GroupLayout logPanelLayout = new javax.swing.GroupLayout(logPanel);
        logPanel.setLayout(logPanelLayout);
        logPanelLayout.setHorizontalGroup(
            logPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
        );
        logPanelLayout.setVerticalGroup(
            logPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
        );

        buttonPanel.setBackground(new java.awt.Color(255, 255, 204));

        exponentialButton.setText("Exponential");
        exponentialButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exponentialButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(exponentialButton);

        gcdButton.setText("GCD");
        gcdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gcdButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(gcdButton);

        factorialButton.setText("Factorial");
        factorialButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                factorialButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(factorialButton);

        placeButton.setText("Place");
        placeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                placeButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(placeButton);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/door_out.png"))); // NOI18N
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(exitButton);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(logPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 623, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(logPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exponentialButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exponentialButtonActionPerformed
        // 2 variables to store exponent and base
        strOutput+=now()+" Last Action: Clicked Exponential Button\n";
        output.setText(strOutput);
        double base;
        int exponent;
        String strBase;
        String strExponent = null;
        try{
            strBase = JOptionPane.showInputDialog(null,"Enter Base Number","Enter Base",JOptionPane.INFORMATION_MESSAGE);         //enter number 
            base=Double.parseDouble(strBase);                                   //parse to int
            strExponent = JOptionPane.showInputDialog(null,"Enter Exponent: ","Enter Exponent",JOptionPane.INFORMATION_MESSAGE); //enter exponent
            exponent=Integer.parseInt(strExponent);                             //parse to double
            strOutput+=now()+" Last Action: Computed Power of "+strBase+" to the power of "+strExponent+" and it is: "+Functions.power(base,exponent)+"\n";
                  
            
        output.setText(strOutput);    
        }
        catch(NumberFormatException nfe){
            JOptionPane.showMessageDialog(null,"Enter a VALID number!","Exception Handled",JOptionPane.ERROR_MESSAGE);
            strOutput+=now()+" Last Action: Exception Handled: "+nfe.getMessage()+"\n";
            output.setText(strOutput);
        }
        
        
    }//GEN-LAST:event_exponentialButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        Functions.exit();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void gcdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gcdButtonActionPerformed
        strOutput+=now()+" Last Action: Clicked GCD Button\n";
        output.setText(strOutput);
        int number1;
        int number2;
        String strNum1;
        String strNum2;
        try{
            strNum1 = JOptionPane.showInputDialog(null,"Enter First Number","Enter number",JOptionPane.INFORMATION_MESSAGE);         //enter number 
            number1=Integer.parseInt(strNum1);                                   //parse to int
            strNum2 = JOptionPane.showInputDialog(null,"Enter Second Number","Enter number",JOptionPane.INFORMATION_MESSAGE); //enter exponent
            number2=Integer.parseInt(strNum2);                             //parse to int
            strOutput+=now()+" Last Action: Computed GCD of "+number1+" and "+number2+" and it is: "+Functions.gcd(number1,number2)+"\n";
                  
            
        output.setText(strOutput);    
        }
        catch(NumberFormatException nfe){
            JOptionPane.showMessageDialog(null,"Enter a VALID number!","Exception Handled",JOptionPane.ERROR_MESSAGE);
            strOutput+=now()+" Last Action: Exception Handled: "+nfe.getMessage()+"\n";
            output.setText(strOutput);
        }
        
        
        
    }//GEN-LAST:event_gcdButtonActionPerformed

    private void factorialButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_factorialButtonActionPerformed
        strOutput+=now()+" Last Action: Clicked Factorial Button\n";
        output.setText(strOutput);
        try{
            long number1; // number entered by user
            String strNum;        // string representation for joptionpane input
                   
            strNum = JOptionPane.showInputDialog(null,"Enter Number","Enter number To get Factorial Of",JOptionPane.INFORMATION_MESSAGE);         //enter number 
            number1=Long.parseLong(strNum);                                   //parse to Long
            
            strOutput+=now()+" Last Action: Computed Factorial of "+number1+" and it is: "+Functions.factorial(number1)+"\n";
                  
            
        output.setText(strOutput);    
        }
        catch(NumberFormatException nfe){
            JOptionPane.showMessageDialog(null,"Enter a VALID ...POSITIVE number!","Exception Handled",JOptionPane.ERROR_MESSAGE);
            strOutput+=now()+" Last Action: Exception Handled: "+nfe.getMessage()+"\n";
            output.setText(strOutput);
        }
    }//GEN-LAST:event_factorialButtonActionPerformed

    private void placeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_placeButtonActionPerformed
       strOutput+=now()+" Last Action: Clicked Place Button\n";
        output.setText(strOutput);
        int number;
        String strNum;
        
        try{
            strNum = JOptionPane.showInputDialog(null,"Enter Number","Enter number to calculate Place",JOptionPane.INFORMATION_MESSAGE);         //enter number 
            number=Integer.parseInt(strNum);                                   //parse to int                            //parse to int
            strOutput+=now()+" Last Action: Computed  of "+number+" and it is: "+Functions.place(number)+"\n";
                  
            
        output.setText(strOutput);    
        }
        catch(NumberFormatException nfe){
            JOptionPane.showMessageDialog(null,"Enter a VALID POSITIVE number!","Exception Handled",JOptionPane.ERROR_MESSAGE);
            strOutput+=now()+" Last Action: Exception Handled: "+nfe.getMessage()+"\n";
            output.setText(strOutput);
        }
    }//GEN-LAST:event_placeButtonActionPerformed

        public static String now() {
        // method uses calendar class to get current date and time for status messages
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(cal.getTime());
// method used for timestamp
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new form().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton exitButton;
    private javax.swing.JButton exponentialButton;
    private javax.swing.JButton factorialButton;
    private javax.swing.JButton gcdButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel logPanel;
    private javax.swing.JTextArea output;
    private javax.swing.JButton placeButton;
    // End of variables declaration//GEN-END:variables
}
